<template>
    <div class="reg">
        <h2>咨询师注册</h2>
        <div class="rform">
            <el-form :model="ruleForm" status-icon :rules="reules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="cname">
                    <el-input v-model="ruleForm.cname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item class="rbtn">
                    <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Register",
        data(){
            var checkCname = (rule, value, callback) => {
                if (value ==='') {
                    callback(new Error('用户名不能为空'));
                }
                setTimeout(() => {
                    callback();
                }, 1000);
               /* setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);*/
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    cname: ''
                },
                reules:{
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    cname: [
                        { validator: checkCname, trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            /*注册提交*/
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    console.log(this.ruleForm.cname)
                    console.log(this.ruleForm.checkPass)
                    if (valid) {
                        this.axios.get("http://localhost:8080/consultant/register",{
                            params:{
                                cname:this.ruleForm.cname,
                                cpassword:this.ruleForm.checkPass
                            }
                        }).then(res=>{
                            console.log(res)
                            if (res.data.data==-1){
                                this.$message({
                                    message: '用户名不能重复',
                                    type: 'warning'
                                });
                            }else{
                                this.$message({
                                    message: '注册成功',
                                    type: 'success'
                                });
                                console.log("你你你你你你你")
                                this.$router.push('/login');
                            }
                        })
                    } else {
                        this.$message({
                            message: '注册失败',
                            type: 'warning'
                        });
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            /*重置表单*/
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .reg{
       text-align: center;
        padding-top: 12%;
        padding-bottom: 18%;
        background-image: url("https://gd-hbimg.huaban.com/559f622332cb30c783488a1cb74ee5fb229007f263521-IwSnHF_fw1200");
        background-size: 100%;
    }
    .rform{
        width: 450px;
        /*height: 500px;*/
        /*background-color: #66ccff;*/
        margin: 0 auto;
    }
    .rbtn{
        margin-right: 20px;
    }
    h2{
        margin-left: 50px;
    }

</style>